package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author atguigu-mqx
 * @ClassName VipStrategy
 * @description: TODO
 * @date 2024年10月14日
 * @version: 1.0
 */
@Service("1003")
public class VipStrategyImpl implements ItemTypeStrategy {

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    /**
     * 记录购买vip数据
     *
     * @param userPaidRecordVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //  购买vip user_vip_service 或 user_info 更新is_vip 字段为 1
        //  创建对象
        UserVipService userVipService = new UserVipService();
        //  赋值：
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setStartTime(new Date());
        //  1. 先知道购买的多长时间
        Long itemId = userPaidRecordVo.getItemIdList().get(0); // vip_service_config.id
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(itemId);
        //  用户购买的时长
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        //  2.用户当前的身份
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        //  创建一个过期时间对象;
        Date exprieTime = new Date();
        //  3.  判断是否需要续期的问题 当前这个用户是vip ，并且未过期要续期：user_info.vip_expire_time + serviceMonth;
        if (userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(new Date())) {
            //  要续期：
            LocalDateTime localDateTime = new LocalDateTime(userInfo.getVipExpireTime());
            //  System.out.println(localDateTime.plusMonths(serviceMonth).toDate());
            //  续期时间
            exprieTime = localDateTime.plusMonths(serviceMonth).toDate();
        } else {
            //  头一次开vip
            exprieTime = new LocalDateTime().plusMonths(serviceMonth).toDate();
        }
        //  不是续期的情况；当前系统时间加购买时长
        //  Date exprieTime = new LocalDateTime().plusMonths(serviceMonth).toDate();
        userVipService.setExpireTime(exprieTime);
        //  设置vip过期时间：
        userVipServiceMapper.insert(userVipService);
        //  手动编写一个运行时异常
//            int i = 1/0;
        //  续期或头一次购买；都应该将用户信息更新为vip用户 is_vip=1; vip_expire_time;
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(exprieTime);
        //  更新用户信息;
        this.userInfoMapper.updateById(userInfo);
    }
}
